$(function(){
	layui.use(['form'],function(){
		 var form = layui.form
		  ,layer = layui.layer;
		 
		 $('#test-get-data').click(function(){
			 var formData = form.val('user-form');
			 console.log(JSON.stringify(formData));
			 
			 var login_id = formData.login_id;
			 var person_name = formData.person_name;
			 var phone_no = formData.phone_no;
			 var email = formData.email;
			 var qqno = formData.qqno;
			 var wechatid = formData.wechatid;
			 var birth = formData.birth;
			 var orgid = $('#orgid').attr('data-value');
			 var allow_login = formData.allow_login;
			 var addr = formData.addr;
			 var person_id = $('#person_id').val();
			 var roles = [];
			 $("input:checkbox[name='role']:checked").each(function(){
				 var role = `{"role_id":"${$(this).val()}"}`
				 roles.push(JSON.parse(role));
			 })
			 
			 if(isNull(login_id)){
				 layer.msg('账号不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(person_name)){
				 layer.msg('姓名不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(phone_no)){
				 layer.msg('手机号码不允许为空!', {icon: 5});
				 return;
			 }

			 if(isNull(orgid)){
				 layer.msg('组织机构不允许为空!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(roles)){
				 layer.msg('请选择用户角色!', {icon: 5});
				 return;
			 }
			 
			 if(isNull(allow_login)){
				 allow_login = 0;
			 }
			 layer.load(2);
			 
			 $.ajax({
				 type : 'post'
				 ,dataType : 'json'
				 ,url : access_url+'/system/systemmanage/updateuser'
				 ,contentType : 'application/json;charset=UTF-8'
				 ,data : JSON.stringify({
					 login_id : login_id
					 ,orgid : orgid
					 ,allow_login : allow_login
					 ,login_roles : roles
					 ,person : {
						 person_id : person_id
						 ,person_name : person_name
						 ,phone_no : phone_no
						 ,email : email
						 ,qqno : qqno
						 ,wechatid : wechatid
						 ,addr : addr
						 ,birth : birth
					 }
				 })
				 ,success : function(data){
					layer.closeAll('loading');
					if(data.code != 200){
						layer.msg(data.msg, {icon: 5});
						return;
					}
					parent.layer.alert(data.msg,{icon:6});
					parent.loadTable();
					parent.layer.closeAll('iframe');
				 }
			 })
		 })
	})
	
})

function initUserInfo(login_id){
	layui.use(['layer','layedit','laydate','form'],function(){
		var layer = layui.layer
		  ,layedit = layui.layedit
		  ,laydate = layui.laydate
		  ,form = layui.form;
		
		laydate.render({
			 elem: '#date'
			 ,trigger: 'click'
		});
		
		layer.load(2);
		$.ajax({
			type : 'post'
			,dataType : 'json'
			,async : false
			,contentType : 'application/json;charset=UTF-8'
			,url : access_url+'/system/systemmanage/queryloginlist'
			,data : JSON.stringify({
				pageNum : 1,
				pageSize : 1,
				params : {
					login_id : login_id
				}
			})
			,success : function(data){
				if(200!=data.code){
					layer.msg(data.msg, {icon: 5});
					parent.layer.closeAll('iframe');
					return;
				}
				var userList = data.retdata.pageData; 
				if(isNull(userList)){
					layer.msg('未获取到用户信息', {icon: 5});
					parent.layer.closeAll('iframe');
					return;
				}
				var userInfo = userList[0];
				var login_id = userInfo.login_id;
				var allow_login = userInfo.allow_login;
				var orgname = '';
				if(!isNull(userInfo.organization)){
					orgname = userInfo.organization.orgname;
				}
				var orgid = userInfo.orgid;
				var roleIds = userInfo.roleIds;
				var person_name = userInfo.person.person_name;
				var phone_no = userInfo.person.phone_no;
				var qqno = userInfo.person.qqno;
				var wechatid = userInfo.person.wechatid;
				var email = userInfo.person.email;
				var birth = userInfo.person.birth;
				var addr = userInfo.person.addr;
				var person_id = userInfo.person.person_id;
				
				$('#login_id').val(login_id);
				$('#person_name').val(person_name);
				$('#phone_no').val(phone_no);
				$('#email').val(email);
				$('#qqno').val(qqno);
				$('#wechatid').val(wechatid);
				$('#date').val(birth);
				$('#orgid').val(orgname);
				$('#orgid').attr('data-value',orgid);
				$('#addr').val(addr);
				$('#person_id').val(person_id);
				
				var allow_btn_html = '';
				if(1==allow_login){
					allow_btn_html = '<input type="checkbox" value="1" checked="" name="allow_login" lay-skin="switch" lay-filter="switchTest" lay-text="允许|禁止"> '
				} else {
					allow_btn_html = '<input type="checkbox" value="1" name="allow_login" lay-skin="switch" lay-filter="switchTest" lay-text="允许|禁止"> '
				}
				$('#allow_btn_div').html(allow_btn_html);
				
				
				var rolesArray = roleIds.split(',');
				var roleLen = rolesArray.length;
				
				$.ajax({
					type : 'get'
					,dataType : 'json'
					,async : false
					,url : access_url+'/system/systemmanage/getallroles'
					,success : function(data){
						console.log("data",data);
						var retlist = data.retlist;
						var len = retlist.length;
						
						var roleshtml = '';
						for (var i = 0; i < len; i++) {
							var role_level = retlist[i].role_level;
							var role_id = retlist[i].role_id;
							var role_name = retlist[i].role_name;
							var role = `<input type="checkbox" name="role" value="${role_id}" title="${role_name}">`;
							
							for (var j = 0; j < roleLen; j++) {
								if(role_id==rolesArray[j]){
									role = `<input type="checkbox" checked="" name="role" value="${role_id}" title="${role_name}">`;
									break;
								}
							}
							roleshtml += role;
						}
						
						$('#roles-item').append(roleshtml);
						
						form.val('user-form');
						layer.closeAll('loading');
					}
				})
			}
		})
		
	})
}

function transmitParam(login_id){
	initUserInfo(login_id);
}

$('#orgid').click(function(){
	layer.open({
		type : 2
		,title : '选择组织机构'
		,area : ['300px','450px']
		,offset : '20px'
		,fixed : false
		,content : 'chooseOrg.html'
		,resize : false
	})
})
